{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption, IRSwap\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.risk import IRDelta\n",
    "from gs_quant.session import Environment, GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solve for Delta\n",
    "Size swap to match swaption delta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "Instrument - Solving - Size one trade by the risk of another trade"
    ]
   },
   "outputs": [],
   "source": [
    "port = Portfolio(\n",
    "    (\n",
    "        IRSwaption('Pay', '5y', 'USD', expiration_date='1y', buy_sell='Sell', name='payer_swaption'),\n",
    "        IRSwap(\n",
    "            'Receive',\n",
    "            '5y',\n",
    "            'USD',\n",
    "            fixed_rate='atm',\n",
    "            notional_amount='=solvefor([payer_swaption].risk.IRDeltaParallel,bp)',\n",
    "            name='hedge1',\n",
    "        ),\n",
    "    )\n",
    ")\n",
    "port.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "port_delta = port.calc(IRDelta(aggregation_level='Type'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# check that delta is approximately equal\n",
    "port_delta['payer_swaption'] - port_delta['hedge1']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
